package com.maluxinyu.easyretry.controller;


import java.util.Random;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.maluxinyu.easyretry.vo.OrderVo;
import com.maluxinyu.easyretry.service.RemoteRetryService;

@RestController
@RequestMapping("/remote")
public class RemoteRetryController {

    @Autowired
    private RemoteRetryService remoteRetryService;
    /**
     * 一个最简单的远程调用案例
     */
    @GetMapping("/retry")
    public void remote(){
        remoteRetryService.remoteRetry();
    }

    @GetMapping("/retryWithLocalRemote")
    public void remoteRetryWithLocalRemote(){
        remoteRetryService.remoteRetryWithLocalRemote(UUID.randomUUID().toString());
    }

    /**
     * 使用自定义的幂等Id生成规则
     */
    @PostMapping("/retryWithIdempotentId")
    public void remoteRetryWithIdempotentId(@RequestBody OrderVo orderVo){
        remoteRetryService.remoteRetryWithIdempotentId(orderVo);
    }

    /**
     * 使用自定义的单参数幂等Id生成规则
     */
    @GetMapping("/retryWithSingleParamIdempotentGenerate")
    public void retryWithSingleParamIdempotentGenerate(){
        remoteRetryService.retryWithSingleParamIdempotentGenerate(String.valueOf(UUID.randomUUID()));
    }

    /**
     * 使用自定义的多参数幂等Id生成规则
     */
    @PostMapping("/retryWithMulParamIdempotentGenerate")
    public void retryWithMulParamIdempotentGenerate(@RequestBody OrderVo orderVo){
        Random random = new Random();
        remoteRetryService.retryWithMulParamIdempotentGenerate(
                String.valueOf(UUID.randomUUID()),
                random.nextInt(),
                random.nextDouble(),
                'a',
                orderVo
        );
    }

    /**
     * 使用自定义的回调方法
     */
    @PostMapping("/retryWithRetryMethod")
    public void remoteRetryWithRetryMethod(@RequestBody OrderVo orderVo){
        remoteRetryService.remoteRetryWithRetryMethod(orderVo);
    }

    /**
     * 使用自定义的回调函数
     */
    @PostMapping("/retryWithCallback")
    public void remoteRetryWithCallback(@RequestBody OrderVo orderVo){
        remoteRetryService.remoteRetryWithCompleteCallback(orderVo);
    }

    /**
     * 指定bizNo
     */
    @PostMapping("/remoteRetryWithBizNo")
    public void remoteRetryWithBizNo(@RequestBody OrderVo orderVo){
        remoteRetryService.remoteRetryWithBizNo(orderVo);
    }


}
